package controller;

import dao.BookDao;
import utils.Book;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet("/BookServlet")
public class BookServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       request.setCharacterEncoding("utf-8");
        String method=request.getParameter("method");
        if(method.equalsIgnoreCase("add"))
            add(request, response);
        else
        if(method.equalsIgnoreCase("delete"))
            delete(request, response);
        else
        if(method.equalsIgnoreCase("update"))
            update(request, response);
        else
        if(method.equalsIgnoreCase("search"))
            search(request, response);
        else
        if(method.equalsIgnoreCase("searchById"))
            searchById(request, response);
        else  if(method.equalsIgnoreCase("searchby")){
            searchBy(request,response);
        }else if(method.equals("discount")){
            discount(request,response);
        }
    }

    private void discount(HttpServletRequest request, HttpServletResponse response) {
        BookDao bookDao = new BookDao();
        bookDao.setMoney();
        search(request,response);
    }

    private void searchBy(HttpServletRequest request, HttpServletResponse response) {
        BookDao bookDao = new BookDao();
        String find = request.getParameter("find");
        Book book = new Book();
        if(null==find||find.trim().equals("")){
            search(request,response);
        }else if(find.matches("\\d")){
            System.out.println("按编号找的..");
             book = bookDao.findbyId(Integer.parseInt(find));
        }else{
            book = bookDao.findbyname(find);
        }
        request.setAttribute("book",book);
        try {
            request.getRequestDispatcher("/book/find.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void search(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("查询所有书籍...");
        BookDao bookDao = new BookDao();
        ArrayList<Book> all = bookDao.findAll();
        request.setAttribute("books",all);
        try {
            request.getRequestDispatcher("book/index.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //增加图书
    private void add(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        Book book = new Book();
        book.setName(req.getParameter("bookname"));
        book.setAuthor(req.getParameter("author"));
        book.setPublisher(req.getParameter("publisher"));
        book.setPrice(Double.parseDouble(req.getParameter("price")));
        book.setUrl(req.getParameter("url"));
        book.setDiscount(Double.parseDouble(req.getParameter("discount")));
        BookDao bookDao = new BookDao();
        bookDao.insert(book);
        search(req,resp);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        int bookid = Integer.parseInt(request.getParameter("bookid"));
        BookDao bookDao = new BookDao();
        bookDao.delete(bookid);
        search(request,response);
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Book book = new Book();
        book.setBook_id(Integer.parseInt(request.getParameter("bookid")));
        book.setName(request.getParameter("bookname"));
        book.setAuthor(request.getParameter("author"));
        book.setPublisher(request.getParameter("publisher"));
        book.setPrice(Double.parseDouble(request.getParameter("price")));
        book.setUrl(request.getParameter("url"));
        book.setDiscount(Double.parseDouble(request.getParameter("discount")));
        System.out.println("更新后的书:"+book);
        request.setAttribute("book",book);
        BookDao bookDao = new BookDao();
        bookDao.update(book);
        search(request,response);
    }

    private void searchById(HttpServletRequest request, HttpServletResponse response) {
        String id = request.getParameter("id");
        BookDao bookDao = new BookDao();
        Book book = bookDao.findbyId(Integer.parseInt(id));
        request.setAttribute("book",book);
        try {
            request.getRequestDispatcher("/book/update.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
